<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="btnCancel">
    <!-- 表单 -->
    <el-form
      ref="addEmployee"
      label-width="120px"
      :model="formdata"
      :rules="rules"
    >
      <el-form-item label="姓名" prop="username">
        <el-input
          v-model="formdata.username"
          style="width: 90%"
          placeholder="请输入姓名"
        />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input
          v-model="formdata.mobile"
          style="width: 90%"
          placeholder="请输入手机号"
        />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="formdata.timeOfEntry"
          style="width: 90%"
          placeholder="请选择入职时间"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          v-model="formdata.formOfEmployment"
          style="width: 90%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in employeeEnum.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input
          v-model="formdata.workNumber"
          style="width: 90%"
          placeholder="请输入工号"
        />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          v-model="formdata.departmentName"
          style="width: 90%"
          placeholder="请选择部门"
          @focus="getDepartments"
        />
        <el-tree
          v-if="showtree"
          :data="depts"
          class="deptsList"
          :props="{label:'name'}"
          :default-expand-all="true"
          @node-click="selectDepartment"
        />
      </el-form-item>
      <el-form-item label="转正时间">
        <el-date-picker
          v-model="formdata.correctionTime"
          style="width: 90%"
          placeholder="请选择转正时间"
        />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { getDepartments } from '@/api/departments'
// 引入 树形组件
import { listToTreeData } from '@/utils'
// 引入枚举
import employeeEnum from '@/api/constant/employees'
// 引入添加员工api
import { addEmployee } from '@/api/employees'

export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formdata: {
        username: '',
        mobile: '',
        formofEmployment: '',
        worknumber: '',
        departmentName: '',
        timeofEntry: '',
        correctionTime: ''
      },
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 4,
            message: '用户姓名为1-4位'
          }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号格式不正确',
            trigger: 'blur'
          }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [{ required: true, message: '入职时间', trigger: 'blur' }]
      },
      depts: [],
      // 63行代码,若是不想在上面解构,也可以在这里进行解构,调用
      // xxx: {
      //   label: 'name'
      // },
      showtree: false,
      employeeEnum: employeeEnum
    }
  },
  mounted() {
    console.log('1111', this.employeeEnum.hireType)
  },
  methods: {
    async getDepartments() {
      // const res = await getDepartments()
      // console.log('111', res)
      // 获取到的数据在 res.depts里面,所以直接 解构 获取
      const { depts } = await getDepartments()
      this.depts = listToTreeData(depts, '')
      // console.log(this.depts)
      this.showtree = true
    },
    // 请选择部门 的下拉选项框
    async selectDepartment(data) {
      this.formdata.departmentName = data.name
      // console.log(this.departmentName)
      this.showtree = false
    },
    async btnOK() {
      // 1.检验表单
      /**
       *validate(确定):对整个表单进行校验的方法，参数为一个回调函数
       该回调函数会在校验结束后被调用，
       传入两个参数：是否校验成功和未通过校验的字段。
       若不传入回调函数，则会返回一个 promise
       *  */
      await this.$refs.addEmployee.validate()
      // 2.发起api请求
      await addEmployee(this.formdata)
      // 3.重新渲染
      // 3.1关闭弹窗
      // 3.2我们是子组件,负责渲染和关闭弹窗的都是 父组件
      // 3.3正常情况下需要 $emit 通知父组件处理
      // this.$emit('getEmployeeList')
      // 另外 this.$parent 可以拿到父组件的属性和方法,直接调用
      this.$parent.getEmployeeList() // 页面刷新
      this.$parent.showDialog = false // 关闭弹窗

      // 4.提示成功
      this.$message.success('操作成功')
    },
    btnCancel() {
      // 1. 清理表单
      this.formdata = {
        username: '',
        mobile: '',
        formofEmployment: '',
        worknumber: '',
        departmentName: '',
        timeofEntry: '',
        correctionTime: ''
      }
      // 2.清理检验(resetFields(恢复源文件):对整个表单进行重置，将所有字段值重置为初始值并移除校验结果)
      this.$refs.addEmployee.resetFields()
      // 关闭弹窗
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style lang="scss">
.deptsList {
  z-index: 99;
  width: 90%;
  height: 200px;
  position: absolute;
  border: 1px solid #ccc;
  overflow-y: auto;
}
</style>
